package com.tianyang.yukuaifu.business.main.view;

import android.os.Build;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;

import com.tianyang.yukuaifu.R;
import com.tianyang.yukuaifu.base.BaseTitleActivity;
import com.tianyang.yukuaifu.common.utils.CommonUtils;
import com.tianyang.yukuaifu.utils.permissions.PermissionsConstant;

import butterknife.BindView;
import butterknife.OnClick;
import per.goweii.anypermission.AnyPermission;
import per.goweii.anypermission.OnPermissionProcess;
import per.goweii.anypermission.RequestListener;

/**
 * Description：系统权限
 */
public class PermissionSettingActivity extends BaseTitleActivity {

    @BindView(R.id.cameraSetTv)
    TextView cameraSetTv;
    @BindView(R.id.noticeSetTv)
    TextView noticeSetTv;
    @BindView(R.id.readAndWriteSetTv)
    TextView readAndWriteSetTv;
    @BindView(R.id.phoneSetTv)
    TextView phoneSetTv;
    @BindView(R.id.locationSetTv)
    TextView locationSetTv;

    private boolean hasNoticeSet;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_permission_setting;
    }

    @Override
    protected void initView() {
        super.initView();
        commonTitle.setLeftTitle("系统权限");
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @Override
    protected void onResume() {
        super.onResume();
        if (CommonUtils.isPermissionEnabled(this, PermissionsConstant.CAMERA)) {
            cameraSetTv.setText("已开启");
        } else {
            cameraSetTv.setText("去设置");
        }
//        if (NotificationUtil.isNotifyEnabled(this)) {
//            hasNoticeSet = true;
//            noticeSetTv.setText("已开启");
//        } else {
//            hasNoticeSet = false;
//            noticeSetTv.setText("去设置");
//        }
        if (CommonUtils.isPermissionEnabled(this, PermissionsConstant.WRITE_STORAGE)) {
            readAndWriteSetTv.setText("已开启");
        } else {
            readAndWriteSetTv.setText("去设置");
        }
//        if (CommonUtils.isPermissionEnabled(this, PermissionsConstant.READ_PHONE_STATE)) {
//            phoneSetTv.setText("已开启");
//        } else {
//            phoneSetTv.setText("去设置");
//        }
//        if (CommonUtils.isPermissionEnabled(this, PermissionsConstant.FINE_LOCATION)
//                && CommonUtils.isPermissionEnabled(this, PermissionsConstant.COARSE_LOCATION)) {
//            locationSetTv.setText("已开启");
//        } else {
//            locationSetTv.setText("去设置");
//        }
    }

    @OnClick({R.id.cameraSetTv, R.id.noticeSetTv, R.id.readAndWriteSetTv, R.id.phoneSetTv, R.id.locationSetTv})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.cameraSetTv:
            case R.id.readAndWriteSetTv:
            case R.id.phoneSetTv:
            case R.id.locationSetTv:
                CommonUtils.openAppSetting(this);
                break;
            case R.id.noticeSetTv:
                if (hasNoticeSet) {
                    CommonUtils.openAppSetting(this);
                } else {
                    requestNotificationShow();
                }
                break;
        }
    }

    private void requestNotificationShow() {
        AnyPermission.with(this).notificationShow()
                .onWithoutPermission(new OnPermissionProcess<Void>() {
                    @Override
                    public void process(@NonNull Void data, @NonNull Processor processor) {
                        processor.next();
                    }
                }).start(new RequestListener() {
                    @Override
                    public void onSuccess() {
                    }

                    @Override
                    public void onFailed() {
                    }
                });
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }
}
